﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace ServiciosWebReniec
{
    /// <summary>
    /// Descripción breve de Service1
    /// </summary>
    [WebService(Namespace = "http://localhost/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // Para permitir que se llame a este servicio Web desde un script, usando ASP.NET AJAX, quite la marca de comentario de la línea siguiente. 
    // [System.Web.Script.Services.ScriptService]
    public class ServiciosReniec : System.Web.Services.WebService
    {

        [WebMethod]
        public string VerificarDNI(string DNIPersona)
        {
            try
            {
                using (SqlConnection myConnection = new SqlConnection(@"Data Source= .\LSQLSERV7;Initial Catalog=reniec;User ID=sa;Password=sql69e$"))
                {
                    myConnection.Open();

                    using (SqlCommand myCommand = new SqlCommand())
                    {
                        myCommand.Connection = myConnection;
                        myCommand.CommandText = "SELECT COUNT(*) FROM Persona WHERE Identificador = @DNIPersona";
                        myCommand.Parameters.Add("@DNIPersona", SqlDbType.VarChar).Value = DNIPersona;
                        return (int)myCommand.ExecuteScalar() == 1 ? "DNI Correcto" : "DNI No Existe";
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return "Ha ocurrido un error.";
            }
        }

        [WebMethod]
        public Persona ObtenerDatosDNI(string DNIPersona)
        {
            Persona persona = new Persona();
            try
            {
                using (SqlConnection myConnection = new SqlConnection(@"Data Source= .\LSQLSERV7;Initial Catalog=reniec;User ID=sa;Password=sql69e$"))
                {
                    myConnection.Open();

                    using (SqlCommand myCommand = new SqlCommand())
                    {
                        myCommand.Connection = myConnection;
                        myCommand.CommandText = "SELECT ApellidoPaterno,ApellidoMaterno,Nombres FROM Persona WHERE Identificador = @DNIPersona";
                        myCommand.Parameters.Add("@DNIPersona", SqlDbType.VarChar).Value = DNIPersona;
                        SqlDataReader personaDato = myCommand.ExecuteReader();
                        if (personaDato.Read())
                        {
                            persona.ApellidoPaterno = personaDato.GetString(0);
                            persona.ApellidoMaterno = personaDato.GetString(1);
                            persona.Nombres = personaDato.GetString(2);
                        }
                        else
                        {
                            throw new Exception("Persona con DNI " + DNIPersona + " no existe.");
                        }
                        personaDato.Close();
                        return persona;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error buscando a la Persona con DNI " + DNIPersona + ": " + ex.Message);
            }
        }

        [WebMethod]
        public String GetLogin(string UserName, string Password)
        {

            try
            {
                using (SqlConnection myConnection = new SqlConnection(@"Data Source= .\LSQLSERV7;Initial Catalog=student;User ID=sa;Password=sql69e$"))
                {
                    myConnection.Open();

                    using (SqlCommand myCommand = new SqlCommand())
                    {
                        myCommand.Connection = myConnection;
                        myCommand.CommandText = "SELECT COUNT(*) FROM Login WHERE UserName = @UserName AND Password = @Password";
                        myCommand.Parameters.Add("@UserName", SqlDbType.VarChar).Value = UserName;
                        myCommand.Parameters.Add("@Password", SqlDbType.VarChar).Value = Password;
                        return (int)myCommand.ExecuteScalar() == 1 ? "success" : "bad username or password";
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return "an error occurred.";
            }

        }
    }
}